#include <iostream>

#include <gtest/gtest.h>

#include "melon/client/thrift_client_pool.h"
#include "bbt/adapters/bbt_favorite_adapter.h"

using melon::client::ClientPool;

using namespace bbt;

TEST(T, Add) {
  int ret = 0;
  ret = BbtFavoriteAdapter::Instance().AddFavorite(127646892, 10001, 2);
  EXPECT_EQ(ret, 1);
  ret = BbtFavoriteAdapter::Instance().AddFavorite(127646892, 10001, 2);
  EXPECT_EQ(ret, 0);
  ret = BbtFavoriteAdapter::Instance().RemoveFavorite(127646892, 10001, 2);
  EXPECT_EQ(ret, 1);
  ret = BbtFavoriteAdapter::Instance().RemoveFavorite(127646892, 10001, 2);
  EXPECT_EQ(ret, 0);

  for(int i = 0; i < 100; ++i) {
    ret = BbtFavoriteAdapter::Instance().AddFavorite(111127646892, 10000000 + i, 2);
    EXPECT_EQ(ret, 1);
  }
  std::vector<FavoriteInfo> favorites; 
  ret = BbtFavoriteAdapter::Instance().GetFavorites(111127646892, &favorites, 2);
  EXPECT_EQ(favorites.size(), 100);
  for(int i = 0; i < 100; ++i) {
    std::cout << favorites[i].engineer << ' ' << favorites[i].timestamp << std::endl;
  }

  for(int i = 0; i < 100; ++i) {
    ret = BbtFavoriteAdapter::Instance().RemoveFavorite(111127646892, 10000000 + i, 2);
    EXPECT_EQ(ret, 1);
  }
  favorites.clear(); 
  ret = BbtFavoriteAdapter::Instance().GetFavorites(111127646892, &favorites, 2);
  EXPECT_EQ(favorites.size(), 0);
}

struct Args {
  Args(int c, char ** v) : argc(c), argv(v) {}
  int argc;
  char** argv;
};

void * ThreadFun(void * p) {
  Args * args = (Args*)p;
  testing::InitGoogleTest(&args->argc, args->argv);
  return (void*)RUN_ALL_TESTS();
}

const int kThreadCount = 1;

int main(int argc, char ** argv) {
  LOG_INIT("./test.log", "DEBUG");
  pthread_t ts[kThreadCount];
  Args args(argc, argv); 
  for (int i = 0; i < kThreadCount; ++i) {
    pthread_create(&ts[i], NULL, ThreadFun, &args);
  }

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_join(ts[i], NULL);
  }
  return 0;
} 

